package com.tl.boot.day1225.controller;

import com.tl.boot.day1225.entity.User;
import com.tl.boot.day1225.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController{

    @Autowired
    private UserService userService;

    @RequestMapping("/getAll")
    public List<User> getAll(){
        return userService.getAll();
    }


//    @RequestMapping(value = "/getUserById/{id}",method = RequestMethod.GET)
//    public User getUserById1(@PathVariable("id") int id){
//        return userService.getUserById(id);
//    }

    //restful风格 获取单个User
    @RequestMapping(value = "/getUserById/{id}",method = RequestMethod.GET)
    public ResponseEntity<User> getUserById(@PathVariable("id") int id){
        try {
            User user = this.userService.getUserById(id);
            if (null==user){
                //相应404，资源不存在
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            //响应200
            return ResponseEntity.ok(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /*@RequestMapping( "/delUserById/{id}")
    public void delUserById(@PathVariable int id){
        userService.delUserById(id);
    }*/

//    @RequestMapping("/add")
//    public void addUser(User user){
//        userService.addUser(user);
//    }

    //Restful风格 添加User
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResponseEntity<Void> addUser(User user){

        try {
            this.userService.addUser(user);
            //响应201
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /*@RequestMapping("/updateUser/{id}")
    public void updateUser(User user){
        int id = user.getId();
        userService.updateUser(user);
    }*/

    //Restful风格 更新User
    @RequestMapping(value = "/updateUser/{id}",method = RequestMethod.PUT)
    public ResponseEntity<Void> updateUser(User user){
        try{
            Integer count = this.userService.updateUser(user);
            if (count.intValue() == 1){
                //响应204
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //Restful风格 删除User
    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseEntity<Void> deleteById(@RequestParam(value = "id",defaultValue = "0") int id){
        try{
            if (id == 0){
                //响应204
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            int count = this.userService.delUserById(id);
            if (count == 1){
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

}
